﻿// See https://aka.ms/new-console-template for more information
using System.Reflection;
using eg._asynchronous;
using eg._for;
using eg._gengeric;
using eg._regexp;
using eg._structs;
using eg._array;
using eg._enum;
using eg._interface_class;
using eg._delegate;
using eg._record;
using eg._attribute;
using eg._tuple;
using eg._reflection;
using nest1;
using static System.Console;

#region 字符串
string _str = Eg_For.egfor(20);
Console.WriteLine(_str);
#endregion

#region 接口
Eg_Interface _interface = new();
#endregion

#region 泛型
Eg_Generic<int> _val = new(20);
Console.WriteLine(_val.value);
Eg_Generic<string> TypeField = new("Hello world!");
Console.WriteLine(TypeField.value);
#endregion

#region 正则
Eg_RegExp _regexp = new();
_regexp.RegExp();
#endregion

#region 结构体
Obj _obj = new();
_obj.init("假如给我三天光明", 43);
_obj.PrintBooksName();
_obj.PrintBooksPrice();
Obj _obj1;
_obj1.bookname = "假如给我三天光明";
_obj1.bookprize = 43;
Console.WriteLine("Book name is {0}, book price is {1}", _obj1.bookname, _obj1.bookprize);
#endregion

#region 数组
Eg_Array _arr = new();
_arr.PrintArray(1);
#endregion

#region 枚举
Eg_Enum _enum = new();
Eg_Enum.PrintEnum();
Console.WriteLine(Eg_Enum.Gender.Transgender);
#endregion

#region 指针
Eg_Pointer _pointer = new();
_pointer.PrintPointer(_pointer.value);
Eg_Pointer.MallocMem("Hello world!");
#endregion

#region 委托
Eg_Delegate _delegate = new();
Eg_Delegate1 _delegate1 = new(_delegate.MyCallback);
Eg_Delegate2 _delegate2 = new(_delegate.AHeavyJob);
_delegate2();
_delegate1.Invoke("Hello world!");
Console.WriteLine(_delegate.fn("Using fn directly."));
dg dg = new(_delegate.fn);
Console.WriteLine(dg("Using fn through delegate."));
Func<int, int, int> sum = delegate (int x, int y)
{
    // WriteLine("");
    return x + y;
};
sum(1, 4);
Action greet = delegate{ Console.WriteLine("Hello world!"); };
greet();
#endregion

#region 记录
Eg_Point _point = new()
{
    X = 123.43,
    Y = 353.34,
    Z = 324.65
};
Console.WriteLine("{0}\n{1}\n{2}", _point.X, _point.Y, _point.Z);
Eg_Person _person = new("Extra", "Fang");
Console.WriteLine(_person.FirstName + " " + _person.LastName);
Eg_Member _member = new(32554)
{
    // Id = 1314,
    Name = "Extra",
    Age = 19
};
_member.Name = "Fang";
System.Console.WriteLine(_member.Id);
#endregion

#region 反射
const string Id = "Id";
Eg_Reflection temp = new(3211, "Extra", 12);
temp.Sex = false;
Type types = temp.GetType();
PropertyInfo[] properties = types.GetProperties();
foreach (PropertyInfo property in properties)
{
    if (property.Name == Id)
    {
        types.GetProperty(Id)?.SetValue(temp, 20);
    }
    Console.WriteLine($"Property name is {property.Name}, value is {property.GetValue(temp)}");
}

foreach (FieldInfo field in types.GetFields())
{
    Console.WriteLine($"Field name is {field.Name}, value is {field.GetValue(temp)}");
}
#endregion

#region 特性
useAttribute attribute = new();
attribute.Test = "Hello world!";
Type ts = attribute.GetType();
foreach (PropertyInfo property in ts.GetProperties())
{
    IEnumerable<LogAttribute> attr = property.GetCustomAttributes<LogAttribute>();
    WriteLine(attr.Count());
    if (attr.Count() > 0)
    {
        WriteLine(attr.ToList()[0].Name);
    }
}
LogAttribute log = (LogAttribute) Attribute.GetCustomAttribute(typeof(useAttribute), typeof(LogAttribute));
WriteLine(log.Name);
#endregion

#region 元组
string[] strs = {"fst", "sec"};
(string fst, string sec) result = Eg_Tuple.TupleAsReturn(strs);
Console.WriteLine("{0}, {1}", result.fst, result.sec);

int[] ArrTuple = { 12, 34 };
(int, int) res = InterChangeWithTuple(ArrTuple);
Console.WriteLine(res);
#endregion

#region 语言自带功能测试
bool _bool = true;
Console.WriteLine(_bool);
char _char = 'x';
Console.WriteLine(_char);

#region ref
int x = 12;
int y = 32;

InterChangePlain(x, y);
Console.WriteLine("Though x is {0}, y is {1}", x, y);

InterChangeWithRef(ref x, ref y);
Console.WriteLine("Now x is {0}, y is {1}", x, y);

ChangeNumberWithRef(ref x);
Console.WriteLine("X is {0}, y is {1}", x, y);
#endregion
#region out
int b;// 只分配内存，不进行初始化
ChangeNumberWithOut(56, 89, out int a/* 也可内联初始化 */, out b);
Console.WriteLine("a is {0}, b is {1}", a, b);
#endregion

#region 字符串转枚举
string? input = "Male";// Console.ReadLine();
if(int.TryParse(input, out int NULL))
{
    Console.WriteLine("Queer");
}
else if(Enum.TryParse(input, out Eg_Enum.Gender jieguo))
{
    Console.WriteLine(jieguo.ToString());
}
else
{
    Console.WriteLine("Queer");
}
#endregion

nest1.nest2.eg.Print();
nest1.eg.Print();
Console.WriteLine(Int32.MaxValue);
WriteLine("Hello world!");
#endregion

#region 异步编程
Eg_Asynchronous Async = new();
//主线程完成后会自动退出，子线程也会被关闭，因此必须加await。
Async.FetchResource();
#endregion

#region 文件读写
StreamWriter sw = new StreamWriter("./Example.txt");
sw.WriteLine("写入文件");
sw.Close();
string line = null;
StreamReader sr = new StreamReader("Example.txt");
line = sr.ReadLine();
while(line != null)
{
    Console.WriteLine(line);
    line = sr.ReadLine();
}
sr.Close();
#endregion
// Console.ReadKey();
Thread.Sleep(5000);

static void InterChangePlain(int x, int y)
{
    int temp = x;
    x = y;
    y = temp;
}
static void InterChangeWithRef(ref int x, ref int y)
{
    int temp = x;
    x = y;
    y = temp;
}
static (int x, int y) InterChangeWithTuple(int[] input)
{
    return (input[1], input[0]);
}

// ref
static void ChangeNumberWithRef(ref int x)
{
    x = 3;
}

// out
// 凡是在out中定义的，都可以在上下文中使用
static void ChangeNumberWithOut(int Inta, int Intb, out int x, out int y)
{
    x = Inta;
    y = Intb;
}

// Eg_Enum.Gender _gender = (Eg_Enum.Gender)Enum.Parse(typeof(Eg_Enum.Gender), i);
// // Eg_Enum.Gender _gender = Eg_Enum.Gender.Transgender;
// switch (_gender)
// {
//     default:
//         Console.WriteLine("Queer");
//         break;
//     case Eg_Enum.Gender.Male:
//         Console.WriteLine(Enum.GetName(Eg_Enum.Gender.Male));
//         break;
//     case Eg_Enum.Gender.Female:
//         Console.WriteLine(Enum.GetName(Eg_Enum.Gender.Female));
//         break;
//     case Eg_Enum.Gender.Transgender:
//         Console.WriteLine(Enum.GetName(Eg_Enum.Gender.Transgender));
//         break;
// }
